package com.greyu.ysj.service.impl;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtPayLog;
import com.greyu.ysj.entity.CbtPayLogExample;
import com.greyu.ysj.mapper.CbtPayLogMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtPayLogService;


@Service
public class CbtPayLogServiceImpl implements CbtPayLogService {
	@Autowired
	private CbtPayLogMapper cbtPayLogMapper;

	@Override
	public List<CbtPayLog> selectAllPayList(CbtPayLog cbtPayLog) {
		CbtPayLogExample cbtPayLogExample=new CbtPayLogExample();
		CbtPayLogExample.Criteria criteria=cbtPayLogExample.createCriteria();
		criteria.andFlagEqualTo(cbtPayLog.getFlag());
		cbtPayLogExample.setOrderByClause("`id` ASC");
		List<CbtPayLog> list=cbtPayLogMapper.selectByExample(cbtPayLogExample);
		return list;
	}

	@Override
	public ResultModel save(CbtPayLog cbtPayLog) {
		try {	
			if ( cbtPayLog.getWalletAddress()==null
					||cbtPayLog.getCbtAmount()==null
					||cbtPayLog.getDrawDate()==null
					||cbtPayLog.getDrawId()==null
					||cbtPayLog.getUserId()==null
	        		) {
	            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
	        }
			//insert
	        this.cbtPayLogMapper.insert(cbtPayLog);
		} catch (Exception e) {
			return ResultModel.error(ResultStatus.INSERT_ERROR);
		}
	        return ResultModel.ok(cbtPayLog);
		}

	@Override
	public CbtPayLog selectByDrawId(int drawId) {
		CbtPayLogExample cbtPayLogExample=new CbtPayLogExample();
		CbtPayLogExample.Criteria criteria=cbtPayLogExample.createCriteria();
		criteria.andDrawIdEqualTo(drawId);
		List<CbtPayLog> list=cbtPayLogMapper.selectByExample(cbtPayLogExample);
		CbtPayLog cbtPayLog;
		 try {
			 cbtPayLog = list.get(0);
	        } catch (Exception e) {
	            return null;
	        }
	        return cbtPayLog;
	}

	@Override
	public ResultModel updateCbtPayLog(Integer id, CbtPayLog cbtPayLog) {
		CbtPayLog newPay=this.cbtPayLogMapper.selectByPrimaryKey(id);
		if(newPay==null) {
			 return ResultModel.error(ResultStatus.PAYLOG_NOT_FOUND);
		}
		if(cbtPayLog.getFlag()!=null) {
			newPay.setFlag(cbtPayLog.getFlag());
		}
		if(cbtPayLog.getPayDate()!=null) {
			newPay.setPayDate(cbtPayLog.getPayDate());
		}
		this.cbtPayLogMapper.updateByPrimaryKey(newPay);
		return ResultModel.ok(newPay);
	}



}
